package com.banmoon.proxy.jdkproxy;

import java.lang.reflect.Proxy;

public class JdkProxyFactory {

    private Object target;

    public JdkProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance(){
        // 简化使用了lambda表达式，需要大家能看懂
        Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (p, m, args) -> {
                    System.out.println("JDK代理开始");
                    Object result = m.invoke(target, args);
                    System.out.println("后置作业通知");
                    return result;
                }
        );
        return proxyInstance;
    }
}
